import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/ext/MessageSelectionControls.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../bloc/MessageSendCubit.dart';
import '../MessagePaneBackgroundView.dart';
import 'MessagePane.dart';

class TextMessagePane extends StatefulWidget {

  final WrapImMessage wrapMessage;

  const TextMessagePane({required this.wrapMessage, super.key});

  @override
  State<StatefulWidget> createState() => _TextMessageSate();
}

class _TextMessageSate extends State<TextMessagePane> {
  final FocusNode _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();

    _focusNode.addListener(() => {if (!_focusNode.hasFocus) {}});
  }

  @override
  Widget build(BuildContext context) {
    // if (_message.messageType != ImMessageType.Text) {
    //   throw new Exception("消息类型不对" + _message.messageType.name);
    // }
    var messageSendCubit = context.read<MessageSendCubit>();
    var messageListCubit = context.read<MessageListCubit>();
    return MessagePaneBackgroundView(
      message: widget.wrapMessage.message,
      child: SelectableText(
        widget.wrapMessage.content,
        focusNode: _focusNode,
        style: const TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE),
        selectionControls: MessageSelectionControls(
            message: widget.wrapMessage.message, messageSendCubit: messageSendCubit, messageListCubit: messageListCubit),
      ),
    );
  }
}

class TextMessageRefPane extends StatelessWidget {
  final ImMessage message;
  final MessageRefParam param;

  const TextMessageRefPane({required this.message, required this.param, super.key});

  @override
  Widget build(BuildContext context) {
    // if (_message.messageType != ImMessageType.Text) {
    //   throw new Exception("消息类型不对" + _message.messageType.name);
    // }

    return Padding(
      padding: const EdgeInsets.only(top: 2, right: 5, bottom: 2, left: 5),
      child: ConstrainedBox(
        constraints: BoxConstraints(maxWidth: param.refMaxLength),
        child: Text(
          _buildContent(),
          maxLines: param.maxLines,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(fontSize: param.fontSize),
        ),
      ),
    );
  }

  String _buildContent() {
    if (param.hasPrefix) {
      return "${(message.fromImUser?.userName ?? '')}:${message.content}";
    }
    return message.content;
  }
}
